package com.ktg.mes.dv.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.common.utils.DateUtils;
import com.ktg.common.utils.SecurityUtils;
import com.ktg.common.utils.StringUtils;
import com.ktg.mes.dv.mapper.DvCheckMachineryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktg.mes.dv.mapper.DvMachineryMapper;
import com.ktg.mes.dv.domain.DvMachinery;
import com.ktg.mes.dv.service.IDvMachineryService;

/**
 * 设备Service业务层处理
 *
 * @author yinjinlu
 * @date 2022-05-08
 */
@Service
public class DvMachineryServiceImpl extends ServiceImpl<DvMachineryMapper, DvMachinery> implements IDvMachineryService
{

    @Autowired
    private DvMachineryMapper dvMachineryMapper;

    /**
     * 查询设备
     *
     * @param machineryId 设备主键
     * @return 设备
     */
    @Override
    public DvMachinery selectDvMachineryByMachineryId(Long machineryId)
    {
        return getById(machineryId);
    }

    /**
     * 查询设备列表
     *
     * @param dvMachinery 设备
     * @return 设备
     */
    @Override
    public List<DvMachinery> selectDvMachineryList(DvMachinery dvMachinery)
    {


        String machinery_type_id = dvMachinery.getMachineryTypeIds();
        dvMachinery.setMachineryTypeIds(null);

        QueryWrapper<DvMachinery> qw = new QueryWrapper<DvMachinery>(dvMachinery);

        qw.like(StringUtils.isNotBlank(dvMachinery.getMachineryCode()),"machinery_code",dvMachinery.getMachineryCode());
        dvMachinery.setMachineryCode(null);
        qw.like(StringUtils.isNotBlank(dvMachinery.getMachineryName()),"machinery_name", dvMachinery.getMachineryName() );
        dvMachinery.setMachineryName(null);
        if (machinery_type_id!=null) {
            List<Integer>  machineryTypeIds = Arrays.stream(machinery_type_id.split(",")).map(Integer::parseInt).collect(Collectors.toList());
            qw.in("machinery_type_id", machineryTypeIds);
        }
        qw.orderByDesc("create_time");
        return list(qw);
    }

    /**
     * 新增设备
     *
     * @param dvMachinery 设备
     * @return 结果
     */
    @Override
    public String insertDvMachinery(DvMachinery dvMachinery)
    {
        DvMachinery dmys=  getBaseMapper().selectDvMachineryByEquipmentCode(dvMachinery.getEquipmentCode());
        if (dmys!=null){
            return "设备号(编码)唯一";
        }
        dvMachinery.setCreateBy(SecurityUtils.getUsername());
        dvMachinery.setCreateTime(DateUtils.getNowDate());
        if (save(dvMachinery)){
            return "成功";
        }
        return "添加失败";
    }

    /**
     * 修改设备
     *
     * @param dvMachinery 设备
     * @return 结果
     */
    @Override
    public String updateDvMachinery(DvMachinery dvMachinery)
    {
        DvMachinery dmys =  getBaseMapper().selectDvMachineryByEquipmentCode(dvMachinery.getEquipmentCode());
        if (dmys!=null){
            if(dmys.getMachineryId().longValue() != dvMachinery.getMachineryId().longValue()){
                return "设备号(编码)唯一";
            }
        }
        dvMachinery.setUpdateBy(SecurityUtils.getUsername());
        dvMachinery.setUpdateTime(DateUtils.getNowDate());
        if (updateById(dvMachinery)){
            return "成功";
        }
        return "修改失败";
    }

    /**
     * 批量删除设备
     *
     * @param machineryIds 需要删除的设备主键
     * @return 结果
     */
    @Override
    public boolean deleteDvMachineryByMachineryIds(Long[] machineryIds)
    {
        return removeByIds(Arrays.asList(machineryIds));
    }

    /**
     * 删除设备信息
     *
     * @param machineryId 设备主键
     * @return 结果
     */
    @Override
    public boolean deleteDvMachineryByMachineryId(Long machineryId)
    {
        return removeById(machineryId);
    }
    /**
     * 查询所有产能大于0的生产设备
     */
    @Override
    public List<DvMachinery> selectProductionEquipmentDvMachineryList() {
        return getBaseMapper().selectProductionEquipmentDvMachineryList();
    }
}
